package com.uniplaza.app.utils;

import android.content.Context;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 各种验证
 */
public class Format {

    //验证email
    public static boolean isEmail(String email) {
        String str = ("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }


//    //验证昵称
//    public static boolean isNick(String nick, Activity cont) {
//
//        if (TextUtils.isEmpty(nick)) {
//            Utils.toast(cont, cont.getString(R.string.nick_empty));
//            return false;
//        }
//
//        nick = nick.trim();
//
//        int lengths = Format.getHanLength(nick);
//        Utils.log("Format", "昵称字节总数是" + lengths);
//        if (lengths >= 4 && lengths <= 30) {
//            return true;
//        } else {
//            Utils.toast(cont, cont.getString(R.string.nick_error));
//            return false;
//        }
//
//    }
//
//    //验证昵称
//    public static boolean isInvate(String nick, Activity cont) {
//
//        if (TextUtils.isEmpty(nick)) {
//            Utils.toast(cont, cont.getString(R.string.invate_empty));
//            return false;
//        }
//
//        return true;
//    }

    //ID
    public static boolean isUid(String nick) {

        if (nick.length() >= 6 && nick.length() <= 20) {
            Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
            Matcher m = p.matcher(nick);
            return m.find();

        } else {
            return false;
        }

    }


    //验证密码
    public static boolean isPassword(String password) {

        if (Format.getHanLength(password) >= 6 && Format.getHanLength(password) <= 16) {
            return true;
        } else {
            return false;
        }

    }

    //是否是纯数字
    public static boolean isFloat(String floats) {

        Utils.log("isFloat", "+++" + floats);
        Pattern p = Pattern.compile("[0-9]+.?[0-9]*$");
        Matcher m = p.matcher(floats);
        if (m.matches()) {
            return true;
        }
        return false;
    }

    public final static boolean isIntiger(String s) {
        if (s != null && !TextUtils.isEmpty(s.trim()))
            return s.matches("^[0-9]*$");
        else
            return false;
    }

    public final static boolean isPhone(String s) {
        if (s != null && !TextUtils.isEmpty(s.trim())) {

            if (s.matches("^[0-9]*$") && s.toString().trim().length() == 11) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    public static String getFormatPhone(String s) {
        if (s != null && !TextUtils.isEmpty(s.trim())) {
            if (s.length() >= 11) {
                StringBuilder builder = new StringBuilder(s);
                return builder.replace(3, 7, "****").toString();
            } else {
                return s;
            }

        } else {
            return s;
        }
    }

    //时间戳转日期
    public static String toDate(String date, Context activity) {
        int diff = Integer.parseInt(date);
        int day = diff / (24 * 60 * 60);
        int hour = (diff / (60 * 60) - day * 24);
        int min = ((diff / (60)) - day * 24 * 60 - hour * 60);
        int seccedn = diff - day * 86400 - hour * 3600 - min * 60;
//        return day + activity.getResources().getString(R.string.day) + hour + activity.getResources().getString(R.string.hour) + min + activity.getResources().getString(R.string.minu)+seccedn+ activity.getResources().getString(R.string.seconds);
        return "";
    }

    //判断是否有特殊符号
    public static boolean hasCode(String date) {
        return date.matches("[0-9a-zA-Z]*");
    }


    public static int getHanLength(String input) {
        int inputlength = input.length();
        String str = input;
        String[] loadLang = new String[1];
        loadLang[0] = "[\\u2E80-\\u9FFF]";//中日韩符号区


        str = replaceAllShow(str, loadLang[0], "");
        Utils.log("计算输入替换后", str);


        int numberlength = str.length();
        int han = inputlength - numberlength;
        Utils.log("计算输入替换后汉字数量", inputlength + "-" + numberlength + "=" + han + "");
        if (han <= 0) {
            han = 0;
        }

        Utils.log("计算输结算后:", (numberlength + (han * 2)) + "");
        return numberlength + (han * 2);
    }


    public static String replaceAllShow(String str, String regex, String newstr) {
        str = str.replaceAll(regex, newstr);

        return str;
    }

    public static String getLangg(String str) {
        if (str.equalsIgnoreCase("ko")) {
            str = "kr";
        }
        return str;
    }

    public static String formatSeconds(int value) {
        int theTime = value;// 秒
        int theTime1 = 0;// 分
        int theTime2 = 0;// 小时
        if (theTime > 60) {
            theTime1 = Integer.valueOf(theTime / 60);
            theTime = Integer.valueOf(theTime % 60);
            if (theTime1 > 60) {
                theTime2 = Integer.valueOf(theTime1 / 60);
                theTime1 = Integer.valueOf(theTime1 % 60);
            }
        }
//        String result = "" + Integer.valueOf(theTime) + "秒";
        String result = "";
        if (theTime1 > 0) {
            result = ":" + Integer.valueOf(theTime1)  ;
        }
        if (theTime2 > 0) {
            result =  Integer.valueOf(theTime2) +  result;
        }
        return result;
    }


}
